package com.kelvin.oocl.countchar;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * 统计文章中的汉子，字母，数字，标点符号
 * @author MAIKE
 *
 */
public class CharCount {
	public static void main(String[] args){
		try {
			String filePath = "./src/main/java/com/kelvin/oocl/countchar/data.txt";
			int[] result = new CharCount().countChar(filePath);
			System.out.println("字母个数： "+result[0]);
			System.out.println("汉字个数： "+result[1]);
			System.out.println("数字个数： "+result[2]);
			System.out.println("标点符号个数： "+result[3]);
			System.out.println("其他字符个数： "+result[4]);
		} catch (FileNotFoundException e) {
			System.err.println(e.getMessage());
		}
	}
	
	public int[] countChar(String filePath) throws FileNotFoundException{
		if(null == filePath){
			return null;
		}
		File file = new File(filePath);
		if(!file.exists()){
			throw new FileNotFoundException("Can not find the file of name "+file.getName());
		}
		int[] result = new int[5];
		int words = 0,letters = 0,punctuations = 0,numbers=0,others=0;
		
		Scanner scanner = new Scanner(file);
		while(scanner.hasNext()){
			String line = scanner.nextLine();
			line = line.trim().replaceAll("\\s+","");
			for(int i=0;i<line.length();i++){
				if(String.valueOf(line.charAt(i)).matches(Expresstion.LETTER_CHAR)){
					letters++;
				}else if(String.valueOf(line.charAt(i)).matches(Expresstion.CHINESS_WORD)){
					words++;
					continue;
				}else if(String.valueOf(line.charAt(i)).matches(Expresstion.NUMBER)){
					numbers++;
				}else if(String.valueOf(line.charAt(i)).matches(Expresstion.PUNCTUATION)){
					punctuations++;
				}else {
					others++;
				}
			}
		}
		
		result[0] = letters;
		result[1] = words;
		result[2] = numbers;
		result[3] = punctuations;
		result[4] = others;
		scanner.close();
		
		return result;
	}
}
